﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Collections.Generic;
using System.Text;

using RFMobile.Database;

namespace RFMobile.Operations
{
    public class OperationData
    {
        private Int32 _comtec_id_operation;
        private Int32 _comtec_id_machine;
        private Int32 _operation_type;
        private String _diagnosis;
        private String _changed_parts;
        private float _value_parts;
        private float _value_work;
        private float _value_others;
        private float _value_total;
        private String _notes;
        private String _notes_for_owner;
        private Int32 _status_reg;
        private DateTime _operation_date_start;
        private DateTime _operation_date_end;
        private Int32 _delay;
        private Int32 _comtec_id_user;

        private DateTime _date_next_maintenance;

        public OperationData()
        {
            
        }

        public void Initialize()
        {
            _value_parts = 0;
            _value_work = 0;
            _value_others = 0;
            _value_total = 0;
            _notes_for_owner = "";
            _status_reg = 1;
            _operation_date_start = DateTime.Now;
        }

        public bool saveOperation()
        {
            bool status = false;
            _operation_date_end = DateTime.Now;
            TimeSpan difftime = _operation_date_end.Subtract(_operation_date_start);
            _delay = difftime.Minutes;

            //Guardar en base datos.
            DatabaseManager db = new DatabaseManager();
            // db.BuildDataSet(new String[] { "operation", "machines" }, "operationSet");
            db.BuildDataSet("operationSet");

            SqlCeDataAdapter da1 = new SqlCeDataAdapter("SELECT * FROM operation", db.Conn);
            SqlCeCommandBuilder builder1 = new SqlCeCommandBuilder(da1);
            SqlCeDataAdapter da2 = new SqlCeDataAdapter("SELECT * FROM machines", db.Conn);
            SqlCeCommandBuilder builder2 = new SqlCeCommandBuilder(da2);

            da1.FillSchema(db.Ds, SchemaType.Source, "operation");
            da1.Fill(db.Ds, "operation");
            da2.FillSchema(db.Ds, SchemaType.Source, "machines");
            da2.Fill(db.Ds, "machines");

            DataRow drowOperation = db.Ds.Tables["operation"].NewRow();
            db.Ds.Tables["machines"].Rows.Find(_comtec_id_machine)["date_next_maintenance"] = _date_next_maintenance;
            
            drowOperation["comtec_id_machine"] = _comtec_id_machine;
            drowOperation["operation_type"] = _operation_type;
            drowOperation["diagnosis"] = _diagnosis;
            drowOperation["changed_parts"] = _changed_parts;
            drowOperation["value_parts"] = _value_parts;
            drowOperation["value_work"] = _value_work;
            drowOperation["value_others"] = _value_others;
            drowOperation["value_total"] = _value_total;
            drowOperation["notes"] = _notes;
            drowOperation["notes_for_owner"] = _notes_for_owner;
            drowOperation["operation_date_start"] = _operation_date_start;
            drowOperation["operation_date_end"] = _operation_date_end;
            drowOperation["delay"] = _delay;
            drowOperation["comtec_id_user"] = _comtec_id_user;
            drowOperation["status_reg"] = _status_reg;

            try
            {
                db.Ds.Tables["operation"].Rows.Add(drowOperation);
                da1.Update(db.Ds, "operation");
                da2.Update(db.Ds, "machines");
                // db.Da.Update(db.Ds, "operation");
                // db.Da.Update(db.Ds, "machines");
                db.Ds.AcceptChanges();
                status = true;
            }
            catch (Exception ex)
            {
                // Log event.
            }
            finally
            {
                db.Close();
            }

            return (status);
        }

        public Int32 comtec_id_operation
        {
            set
            {
                _comtec_id_operation = value;
            }
            get
            {
                return (_comtec_id_operation);
            }
        }

        public Int32 comtec_id_machine
        {
            set
            {
                _comtec_id_machine = value;
            }
            get
            {
                return (_comtec_id_machine);
            }
        }

        public Int32 operation_type
        {
            set
            {
                _operation_type = value;
            }
            get
            {
                return (_operation_type);
            }
        }

        public String diagnosis
        {
            set
            {
                _diagnosis = value;
            }
            get
            {
                return (_diagnosis);
            }
        }

        public String changed_parts
        {
            set
            {
                _changed_parts = value;
            }
            get
            {
                return (_changed_parts);
            }
        }

        public String notes
        {
            set
            {
                _notes = value;
            }
            get
            {
                return (_notes);
            }
        }

        public Int32 comtec_id_user
        {
            set
            {
                _comtec_id_user = value;
            }
            get
            {
                return (_comtec_id_user);
            }
        }

        public DateTime next_maintainment_date
        {
            set
            {
                _date_next_maintenance = value;
            }
            get
            {
                return (_date_next_maintenance);
            }
        }
    }
}
